.Dd July 17, 2014
.Dt BF_SET_KEY 3
.Os
.Sh NAME
.Nm BF_set_key ,
.Nm BF_encrypt ,
.Nm BF_decrypt ,
.Nm BF_ecb_encrypt ,
.Nm BF_cbc_encrypt ,
.Nm BF_cfb64_encrypt ,
.Nm BF_ofb64_encrypt ,
.Nm BF_options
.Nd Blowfish encryption
.Sh SYNOPSIS
.In openssl/blowfish.h
.Ft void
.Fo BF_set_key
.Fa "BF_KEY *key"
.Fa "int len"
.Fa "const unsigned char *data"
.Fc
.Ft void
.Fo BF_ecb_encrypt
.Fa "const unsigned char *in"
.Fa "unsigned char *out"
.Fa "BF_KEY *key"
.Fa "int enc"
.Fc
.Ft void
.Fo BF_cbc_encrypt
.Fa "const unsigned char *in"
.Fa "unsigned char *out"
.Fa "long length"
.Fa "BF_KEY *schedule"
.Fa "unsigned char *ivec"
.Fa "int enc"
.Fc
.Ft void
.Fo BF_cfb64_encrypt
.Fa "const unsigned char *in"
.Fa "unsigned char *out"
.Fa "long length"
.Fa "BF_KEY *schedule"
.Fa "unsigned char *ivec"
.Fa "int *num"
.Fa "int enc"
.Fc
.Ft void
.Fo BF_ofb64_encrypt
.Fa "const unsigned char *in"
.Fa "unsigned char *out"
.Fa "long length"
.Fa "BF_KEY *schedule"
.Fa "unsigned char *ivec"
.Fa "int *num"
.Fc
.Ft const char *
.Fo BF_options
.Fa void
.Fc
.Ft void
.Fo BF_encrypt
.Fa "BF_LONG *data"
.Fa "const BF_KEY *key"
.Fc
.Ft void
.Fo BF_decrypt
.Fa "BF_LONG *data"
.Fa "const BF_KEY *key"
.Fc
.Sh DESCRIPTION
This library implements the Blowfish cipher,
which was invented and described by
.An Counterpane .
.Pp
Blowfish is a block cipher that operates on 64 bit (8 byte) blocks of data.
It uses a variable size key, but typically, 128 bit (16 byte) keys
are considered good for strong encryption.
Blowfish can be used in the same modes as DES (see
.Xr des_modes 3 ) .
Blowfish is currently one of the faster block ciphers.
It is quite a bit faster than DES, and much faster than IDEA or RC2.
.Pp
Blowfish consists of a key setup phase
and the actual encryption or decryption phase.
.Pp
.Fn BF_set_key
sets up the
.Vt BF_KEY
.Fa key
using the
.Fa len
bytes long key at
.Fa data .
.Pp
.Fn BF_ecb_encrypt
is the basic Blowfish encryption and decryption function.
It encrypts or decrypts the first 64 bits of
.Fa in
using the key
.Fa key ,
putting the result in
.Fa out .
.Fa enc
decides if encryption
.Pq Dv BF_ENCRYPT
or decryption
.Pq Dv BF_DECRYPT
shall be performed.
The vector pointed at by
.Fa in
and
.Fa out
must be 64 bits in length, no less.
If they are larger, everything after the first 64 bits is ignored.
.Pp
The mode functions
.Fn BF_cbc_encrypt ,
.Fn BF_cfb64_encrypt ,
and
.Fn BF_ofb64_encrypt
all operate on variable length data.
They all take an initialization vector
.Fa ivec
which needs to be passed along into the next call of the same function
for the same message.
.Fa ivec
may be initialized with anything, but the recipient needs to know what
it was initialized with, or it won't be able to decrypt.
Some programs and protocols simplify this, like SSH, where
.Fa ivec
is simply initialized to zero.
.Fn BF_cbc_encrypt
operates on data that is a multiple of 8 bytes long, while
.Fn BF_cfb64_encrypt
and
.Fn BF_ofb64_encrypt
are used to encrypt an variable number of bytes (the amount
does not have to be an exact multiple of 8).
The purpose of the latter two is to simulate stream ciphers,
and therefore, they need the parameter
.Fa num ,
which is a pointer to an integer where the current offset in
.Fa ivec
is stored between calls.
This integer must be initialized to zero when
.Fa ivec
is initialized.
.Pp
.Fn BF_cbc_encrypt
is the Cipher Block Chaining function for Blowfish.
It encrypts or decrypts the 64 bits chunks of
.Fa in
using the key
.Fa schedule ,
putting the result in
.Fa out .
.Fa enc
decides if encryption
.Pq Dv BF_ENCRYPT
or decryption
.Pq Dv BF_DECRYPT
shall be performed.
.Fa ivec
must point at an 8 byte long initialization vector.
.Pp
.Fn BF_cfb64_encrypt
is the CFB mode for Blowfish with 64 bit feedback.
It encrypts or decrypts the bytes in
.Fa in
using the key
.Fa schedule ,
putting the result in
.Fa out .
.Fa enc
decides if encryption
.Pq Dv BF_ENCRYPT
or decryption
.Pq Dv BF_DECRYPT
shall be performed.
.Fa ivec
must point at an
8 byte long initialization vector.
.Fa num
must point at an integer which must be initially zero.
.Pp
.Fn BF_ofb64_encrypt
is the OFB mode for Blowfish with 64 bit feedback.
It uses the same parameters as
.Fn BF_cfb64_encrypt ,
which must be initialized the same way.
.Pp
.Fn BF_encrypt
and
.Fn BF_decrypt
are the lowest level functions for Blowfish encryption.
They encrypt/decrypt the first 64 bits of the vector pointed by
.Fa data ,
using the key
.Fa key .
These functions should not be used unless you implement 'modes' of Blowfish.
The alternative is to use
.Fn BF_ecb_encrypt .
If you still want to use these functions, you should be aware
that they take each 32-bit chunk in host-byte order,
which is little-endian on little-endian platforms
and big-endian on big-endian ones.
.Sh RETURN VALUES
None of the functions presented here return any value.
.Sh NOTE
Applications should use the higher level functions
.Xr EVP_EncryptInit 3
etc. instead of calling the blowfish functions directly.
.Sh HISTORY
The Blowfish functions are available in all versions of SSLeay and OpenSSL.
